<?php

// 创建一个udp 的服务

$http = new swoole_http_server('0.0.0.0',8811);


$http->set(
	[
		'enable_static_handler' => true,
        'document_root' => "/home/wwwroot/live/public/static/live",
		'worker_num' => 5,
	]
);

$http->on("WorkerStart",function(swoole_server $server,  $worker_id){
	
	// echo __DIR__;
	// echo "worker start ";
	// 定义应用框架
	define("APP_PATH",__DIR__."/../application/");
	
	// 加载框架里面的文件
	require __DIR__."/../thinkphp/base.php";
	
});


/** */

$http->on('request',function($request, $response) use($http){
	
	//$response->cookie('fwz','fengzi',time()+1800);  // 设置cookie
	//$response->end("ssss".json_encode($request->get));
	
	$_SERVER  =  [];
	if(isset($request->server)) {
		foreach($request->server as $k => $v) {
			$_SERVER[strtoupper($k)] = $v;
		}
	}
	if(isset($request->header)) {
		foreach($request->header as $k => $v) {
			$_SERVER[strtoupper($k)] = $v;
		}
	}
	//echo APP_PATH.PHP_EOL;
	
	// print_r($_SERVER);
		
	$_GET = [];
	if(isset($request->get)) {
		foreach($request->get as $k => $v) {
			$_GET[$k] = $v;
		}
	}
	
	$_POST = [];
	if(isset($request->post)) {
		foreach($request->post as $k => $v) {
			$_POST[$k] = $v;
		}
	}
	
	ob_start();
	try{
		think\App::run()->send();
		//think\Container::get('app', [APP_PATH])->run()->send();
		
	}catch(\Exception $e){
		// 异常处理
	}
	
	// echo "--action--".request()->action().PHP_EOL;
	// print_r($_SERVER);
	$res = ob_get_contents();
    ob_end_clean();
	
    $response->end($res);
	
});


$http->start();
